﻿// Copyright (c) 2020-2021 百小僧, Baiqian Co.,Ltd.
// TimeCrontab is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//             https://gitee.com/dotnetchina/TimeCrontab/blob/master/LICENSE
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

using System;

namespace TimeCrontab
{
    /// <summary>
    /// Cron 字段值含 * 字符解析器
    /// </summary>
    /// <remarks>
    /// <para>* 表示任意值，该字符支持在 Cron 所有字段域中设置</para>
    /// </remarks>
    internal sealed class AnyParser : ICronParser, ITimeParser
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="kind">Cron 字段种类</param>
        public AnyParser(CrontabFieldKind kind)
        {
            Kind = kind;
        }

        /// <summary>
        /// Cron 字段种类
        /// </summary>
        public CrontabFieldKind Kind { get; }

        /// <summary>
        /// 判断当前时间是否符合 Cron 字段种类解析规则
        /// </summary>
        /// <param name="datetime">当前时间</param>
        /// <returns><see cref="bool"/></returns>
        public bool IsMatch(DateTime datetime)
        {
            return true;
        }

        /// <summary>
        /// 获取 Cron 字段种类当前值的下一个发生值
        /// </summary>
        /// <param name="currentValue">时间值</param>
        /// <returns><see cref="int"/></returns>
        /// <exception cref="TimeCrontabException"></exception>
        public int? Next(int currentValue)
        {
            // 由于天、月、周计算复杂，所以这里排除对它们的处理
            if (Kind == CrontabFieldKind.Day
                || Kind == CrontabFieldKind.Month
                || Kind == CrontabFieldKind.DayOfWeek)
            {
                throw new TimeCrontabException("Cannot call Next for Day, Month or DayOfWeek types.");
            }

            // 默认递增步长为 1
            int? newValue = currentValue + 1;

            // 验证最大值
            var maximum = Constants.MaximumDateTimeValues[Kind];
            return newValue > maximum ? null : newValue;
        }

        /// <summary>
        /// 获取 Cron 字段种类字段起始值
        /// </summary>
        /// <returns><see cref="int"/></returns>
        /// <exception cref="TimeCrontabException"></exception>
        public int First()
        {
            // 由于天、月、周计算复杂，所以这里排除对它们的处理
            if (Kind == CrontabFieldKind.Day
                || Kind == CrontabFieldKind.Month
                || Kind == CrontabFieldKind.DayOfWeek)
            {
                throw new TimeCrontabException("Cannot call First for Day, Month or DayOfWeek types.");
            }

            return 0;
        }

        /// <summary>
        /// 将解析器转换成字符串输出
        /// </summary>
        /// <returns><see cref="string"/></returns>
        public override string ToString()
        {
            return "*";
        }
    }
}